
/**
 * Video with all informations
 */
var VideoItem = function(data) {
	
	this.link = data.linkEmbed;
	this.id = data.id;
	this.title = data.title;
	this.image = data.image;
	this.favecount = data.favecount;
	this.rome = data.rome;
	this.data = data;
	this.linkYoutube = data.link;
	
	this.dom = null;
	var $this = this;
	

	
	/**
	 * Create the list item dom element displayed
	 */
	function createDOM() {
		var faves = "";
		if (typeof($this.favecount) != "undefined") {
			faves = $this.favecount+" FAVEs";
		}
		var title = getTitle();
		var options = MikuClip.options;
		/*
		var link = $this.link;
		if (options.youtubeQuality != "default") {
			link += "&vq="+options.youtubeQuality;
		}
		*/
		var link = $this.link;
		var tpl = '<li class="video-item"><a href="'+link+'" data-transition="slide">'+
			'<img src="'+$this.image+'">'+
			'<h2>'+title+'</h2>'+
			'<p>'+faves+'</p>'+
		'</a></li>';
		return $(tpl);
	}
	
	/**
	 * Add events to the elements of the dom
	 */
	this.addEvents = function() {
		$($this.dom).on("click", "a", onClickLink);
	}
	
	/**
	 * Click on a link
	 */
	function onClickLink(e) {
		WebappHistory.push();
		$this.fillPageVideoDetail();
		
		$.mobile.changePage("#page-video",{transition: 'slide'});
		e.preventDefault();
		return false;
	}
	
	/**
	 * Get the title displayed
	 */
	function getTitle() {
		var title = $this.title;
		if (typeof($this.rome) != "undefined" && $this.rome != "") {
			title = $this.rome;
		}
		return title;
	}
	
	
	/**
	 * Fill the detail page with the video data
	 */
	this.fillPageVideoDetail = function(e) {
		
		var link = $this.link;
		var options = MikuClip.options;
		if (options.youtubeQuality != "default") {
			link += "&vq="+options.youtubeQuality;
		}
		$("#page-video #video-object").attr("data", link);
		$("#page-video #video-title").html(getTitle());
		$("#page-video #video-original-title").html($this.title);
		$("#page-video #video-faves").html($this.favecount);
		$("#page-video #video-id").val($this.id);
		PageVideo.setLastVideo($this.data);

	}
	
	
	this.dom = createDOM();
	this.addEvents();
};